#pragma once

#include <iostream>
#include <string>
#include <ctemplate/template.h>

#include "oj_controller.hpp"
// #include "oj_model.hpp"
#include "oj_model_mysql.hpp"


namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./wwwRoot/html/";

    class View
    {
    public: // 构造 + 析构
        View(){}
        ~View(){}
    public:
        // 将全部题目信息渲染到html中
        // questions 输入型参数 html 输出型参数
        void AllExpandHtml(const vector<Question> &questions, std::string* html) 
        {
            // 1. 形成路径
            std::string src_html = template_path + "all_questions.html";
            // 2. 将questions的内容形成数字典
            ctemplate::TemplateDictionary dict("all_questions");
            for(const auto& q : questions)
            {
                ctemplate::TemplateDictionary *tmp = dict.AddSectionDictionary("question_list");
                tmp->SetValue("number", q.number);
                tmp->SetValue("title", q.title);
                tmp->SetValue("level", q.level);
                tmp->SetValue("type", q.type);
            }

            // 3. 获取渲染后的html (DO_NOT_STRIP 表示不删除html中的空格和换行符)
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);
            // 4. 渲染
            tpl->Expand(html, &dict);
        }

        // 将单个题目信息渲染到html中
        // question 输入型参数 html 输出型参数
        void SingleExpandHtml(const Question &q, std::string* html)
        {
            // 1. 形成路径
            std::string src_html = template_path + "single_question.html";
            // 2. 将该题目的相关内容形成数字典
            ctemplate::TemplateDictionary dict("single_question");
            dict.SetValue("number", q.number);
            dict.SetValue("title", q.title);
            dict.SetValue("level", q.level);
            dict.SetValue("desc", q.desc);
            dict.SetValue("pre_code", q.header);

            LOG_DEBUG() << "pre_code: " << q.header;
            // 3. 获取选然后的html                                                                                         
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);
            // 4. 完成渲染
            tpl->Expand(html, &dict);
        }
    };
}
